package com.guo.rpc.invoke;

import com.guo.rpc.bean.ServiceBean;
import com.guo.rpc.common.AbstactBeanConfig;
import com.guo.rpc.common.BeanConfig;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Author: ljguo4
 * @email: ljguo4@iflytek.com
 * @Date: 2021/8/9 20:24
 * @Description:
 * @version: 1.0
 */
public class ServerInvoker implements Invoker{

    @Override
    public InvokeResult invoke(Invocation invocation) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        ServiceBean serviceBean = BeanConfig.getServiceBean(invocation.getInterfaceClass());
        Object ref = serviceBean.getRef();
        Class<?> serviceClass = ref.getClass();
        Method method = serviceClass.getMethod(invocation.getMethodName(), invocation.getParameterTypes());
        Object invoke = method.invoke(ref, invocation.getParameters());

        InvokeResult invokeResult = new InvokeResult();
        invokeResult.setResultData(invoke);
        invokeResult.setResultType(method.getReturnType());
        return invokeResult;
    }
}
